package com.ruitu.health_1.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.ruitu.health_1.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by  on 2018/4/14.
 */

public class EvaluateView extends RelativeLayout implements View.OnClickListener {
    private Context mContext;
    private ImageView iv_comment_star_1;
    private ImageView iv_comment_star_2;
    private ImageView iv_comment_star_3;
    private ImageView iv_comment_star_4;
    private ImageView iv_comment_star_5;

    private int currentStarCount = 0;
    private List<ImageView> imageViews;
    private OnEvaluateStarListener onEvaluateStarListener;

    public EvaluateView(Context context) {
        super(context);
        initView(context);
    }


    public EvaluateView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public EvaluateView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        this.mContext = context;
        imageViews = new ArrayList<>();
        View view = inflate(mContext, R.layout.item_evaluate_view, this);
        iv_comment_star_1 = view.findViewById(R.id.iv_comment_star_1);
        iv_comment_star_2 = view.findViewById(R.id.iv_comment_star_2);
        iv_comment_star_3 = view.findViewById(R.id.iv_comment_star_3);
        iv_comment_star_4 = view.findViewById(R.id.iv_comment_star_4);
        iv_comment_star_5 = view.findViewById(R.id.iv_comment_star_5);
        iv_comment_star_1.setOnClickListener(this);
        iv_comment_star_2.setOnClickListener(this);
        iv_comment_star_3.setOnClickListener(this);
        iv_comment_star_4.setOnClickListener(this);
        iv_comment_star_5.setOnClickListener(this);
        imageViews.add(iv_comment_star_1);
        imageViews.add(iv_comment_star_2);
        imageViews.add(iv_comment_star_3);
        imageViews.add(iv_comment_star_4);
        imageViews.add(iv_comment_star_5);

        currentStarCount = 5;
    }

    @Override
    public void onClick(View v) {
        if (v == iv_comment_star_1) {
            currentStarCount = 1;
        }
        if (v == iv_comment_star_2) {
            currentStarCount = 2;
        }
        if (v == iv_comment_star_3) {
            currentStarCount = 3;
        }
        if (v == iv_comment_star_4) {
            currentStarCount = 4;
        }
        if (v == iv_comment_star_5) {
            currentStarCount = 5;
        }
        for (int i = 0; i < imageViews.size(); i++) {
            imageViews.get(i).setImageResource(i < currentStarCount ? R.mipmap.icon_xingji : R.mipmap.icon_xingjiji);
        }
        onEvaluateStarListener.OnStarNum(currentStarCount);
    }

    public interface OnEvaluateStarListener {
        void OnStarNum(int num);
    }
    public void setOnEvaluateStar(OnEvaluateStarListener onEvaluateStar){
        this.onEvaluateStarListener = onEvaluateStar;
    }
}
